package de.tum.in.eist.poll.server.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import de.tum.in.eist.poll.shared.beans.Student;
import de.tum.in.eist.poll.shared.beans.User;

/**
 * @author The EIST-POLL-21 team
 *
 */

public class UserDaoImpl implements UserDaoInterface, Serializable {
	/**
	 * serialization version
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * My Doc.
	 */
	private HashMap<String,User> users = new HashMap<String,User>();
	
	public UserDaoImpl() {
	}

	@Override
	public void add(User s) {
		users.put(s.getUserID(), s);
	}


	@Override
	public User get(String userID) {
		return users.get(userID);
	}
	
	public List<Student> getStudentsByLecturer(String lecturerID) {
		List<Student> result = new ArrayList<Student>();
		for (User user : users.values()) {
			if (user instanceof Student	&& ((Student)user).getLecturerID().equals(lecturerID)) {
				result.add((Student)user);
			}
		}
		return result;
	}

	@Override
	public List<User> getAll() {
		ArrayList<User> result = new ArrayList<User>();
		for (String userID: users.keySet()) {
			result.add(users.get(userID));
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see de.tum.in.eist.poll.server.PersistentUser#exists(int)
	 */
	@Override
	public boolean exists(String userID) {
		return users.containsKey(userID);
	}

	/* (non-Javadoc)
	 * @see de.tum.in.eist.poll.server.PersistentUser#deleteUser(int)
	 */
	@Override
	public void delete(String userID) {
		if (exists(userID)) {
			users.remove(userID);
		}
	}
	
	@Override
	public int numberOfUsers() {
		return users.size();
	}
}